package dashboard.controllers;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;
import org.apache.velocity.io.VelocityWriter;

import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.BeatContext;
import com.bj58.wf.mvc.MvcConstants;

public class ViewResult extends ActionResult {

	private final String prefix = MvcConstants.VIEW_PREFIX;
	private final String suffix = ".html";
	private final String viewName;
	private String contentType = "text/html;charset=\"UTF-8\"";

	public ViewResult(String viewName) {
		this.viewName = viewName;
	}

	public ViewResult(String viewName, String contentType) {
		this.viewName = viewName;
		this.contentType = contentType;
	}

	/**
	 * @return the viewName
	 */
	public String getViewName() {
		return viewName;
	}

	public void render(BeatContext beat, Template template, Context context, String path) throws Exception {
		VelocityWriter vw = new VelocityWriter(beat.getResponse().getWriter());
		try {
			template.merge(context, vw);
			vw.flush();
		} finally {
			vw.recycle(null);
		}
	}

	@Override
	public void render(BeatContext beat) throws Exception {

		String path = prefix + "\\" + viewName;
		if (!viewName.endsWith(suffix)) {
			path += suffix;
		}

		Template template = Velocity.getTemplate(path);
		HttpServletResponse response = beat.getResponse();
		response.setContentType(this.contentType);
		response.setCharacterEncoding("UTF-8");
		// init context:
		Context context = new VelocityContext(beat.getModel().getModel());

		render(beat, template, context, path);

	}

	public static ActionResult jsonView(String json) {
		final String json1 = json; // jq1.4只识别双引号
		return new ActionResult() {
			@Override
			public void render(BeatContext context) throws Exception {
				context.getResponse().setContentType("application/json;charset=utf-8");
				context.getResponse().addHeader("Access-Control-Allow-Origin", "*"); // 解决跨域问题
				context.getResponse().getWriter().write(json1);
				context.getResponse().getWriter().flush();
			}
		};

	}

	private static final String jsonp_Patten = "%s(%s);";
	private static final String callbackname = "callback";

	public static ActionResult jsonpView(String json) {
		final String json1 = json; // jq1.4只识别双引号
		return new ActionResult() {
			@Override
			public void render(BeatContext context) throws Exception {
				context.getResponse().setContentType("application/json;charset=utf-8");
				context.getResponse().addHeader("Access-Control-Allow-Origin", "*"); // 解决跨域问题
				String callback = context.getRequest().getParameter(callbackname);
				if (StringUtils.isBlank(callback)) {
					context.getResponse().getWriter().write(json1);
				} else {
					context.getResponse().getWriter().write(String.format(jsonp_Patten, callback, json1));
				}
				context.getResponse().getWriter().flush();
			}
		};

	}

}
